
package com.platform.modules.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.Constant;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.StringUtils;
import com.platform.common.utils.ValidUtils;
import com.platform.modules.mall.entity.MallOrderEntity;
import com.platform.modules.mall.entity.MallOrderGoodsEntity;
import com.platform.modules.mall.entity.MallShopsEntity;
import com.platform.modules.mall.service.MallOrderGoodsService;
import com.platform.modules.mall.service.MallOrderService;
import com.platform.modules.mall.service.MallShopsService;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.service.SysUserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 订单表Controller
 *
 * @author
 * @date 2019-07-05 19:29:18
 */
@RestController
@RequestMapping("mall/order")
public class MallOrderController extends AbstractController {
    @Autowired
    private MallOrderService mallOrderService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private MallOrderGoodsService orderGoodsService;
    @Autowired
    private MallShopsService shopsService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("mall:order:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<MallOrderEntity> list = mallOrderService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/export")
    @RequiresPermissions("mall:order:list")
    public RestResponse export(@RequestParam Map<String, Object> params) {

        Integer shippingStatus = Integer.valueOf((String)params.get("shippingStatus"));

        if(shippingStatus != null ){

            switch (shippingStatus){
                case 1:
                    params.put("orderStatus",Constant.OrderStatus.YFK.getValue());
                    break;
                case 2:
                    params.put("orderStatus",Constant.OrderStatus.YFH.getValue());
                    break;
                case 3:
                    params.put("orderStatus",Constant.OrderStatus.QRSH.getValue());
                    break;
                case 4:
                    params.put("orderStatuslist", "401,402");
//                    params.put("orderStatus",Constant.OrderStatus.TK.getValue());
                    break;
                default:  break;
            }
        }else {
            params.put("orderStatuslist", "0,201,300,301,401,402");
        }

        List<MallOrderEntity> list = mallOrderService.export(params);

//        params.put("orderStatuslist", "0,201,300,301,401,402");
//        List<MallOrderEntity> list = mallOrderService.export(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询订单表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("mall:order:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page page = mallOrderService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:order:info")
    public RestResponse info(@PathVariable("id") String id) {
        MallOrderEntity mallOrder = mallOrderService.queryById(id);

        List<MallOrderGoodsEntity> orderGoodsEntityList = orderGoodsService.list(new QueryWrapper<MallOrderGoodsEntity>()
                .eq("ORDER_ID", mallOrder.getId()));
        mallOrder.setOrderGoodsEntityList(orderGoodsEntityList);
        return RestResponse.success().put("order", mallOrder);
    }

    /**
     * 新增订单表
     *
     * @param mallOrder mallOrder
     * @return RestResponse
     */
    @SysLog("新增订单表")
    @RequestMapping("/save")
    @RequiresPermissions("mall:order:save")
    public RestResponse save(@RequestBody MallOrderEntity mallOrder) {

        mallOrderService.add(mallOrder);

        return RestResponse.success();
    }

    /**
     * 修改订单表
     *
     * @param mallOrder mallOrder
     * @return RestResponse
     */
    @SysLog("修改订单表")
    @RequestMapping("/update")
    @RequiresPermissions("mall:order:update")
    public RestResponse update(@RequestBody MallOrderEntity mallOrder) {

        mallOrderService.update(mallOrder);

        return RestResponse.success();
    }

    /**
     * 根据主键删除订单表
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除订单表")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:order:delete")
    public RestResponse delete(@RequestBody String[] ids) {
        mallOrderService.deleteBatch(ids);

        return RestResponse.success();
    }

    /**
     * 分页查询我的订单
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/myOrder")
    @RequiresPermissions("mall:order:myOrder")
    public RestResponse myOrder(@RequestParam Map<String, Object> params) {
        //获取用户所属的角色列表
        List<String> roleIdList = sysUserRoleService.queryRoleIdList(this.getUserId());
        if (roleIdList.contains(Constant.ORG_ADMIN)) {
            params.put("orgNo", this.getOrgNo());
        } else if (roleIdList.contains(Constant.SHOPS_ADMIN)) {
            MallShopsEntity shopsEntity = shopsService.getOne(new LambdaQueryWrapper<MallShopsEntity>().select(MallShopsEntity::getId)
                    .eq(MallShopsEntity::getUserId, getUserId()));
            if (shopsEntity == null || StringUtils.isBlank(shopsEntity.getId())) {
                return RestResponse.error("没有权限，请联系管理员！");
            }
            params.put("parentId", shopsEntity.getId());
        } else {
            params.put("sysUserId", getUserId());
        }
        Page page = mallOrderService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/exportMyOrder")
    @RequiresPermissions("mall:order:myOrder")
    public RestResponse exportMyOrder(@RequestParam Map<String, Object> params) {
        params.put("orderStatuslist", "0,201,300,301,401,402");
        //获取用户所属的角色列表
        List<String> roleIdList = sysUserRoleService.queryRoleIdList(this.getUserId());
        if (roleIdList.contains(Constant.ORG_ADMIN)) {
            params.put("orgNo", this.getOrgNo());
        } else if (roleIdList.contains(Constant.SHOPS_ADMIN)) {
            MallShopsEntity shopsEntity = shopsService.getOne(new LambdaQueryWrapper<MallShopsEntity>().select(MallShopsEntity::getId)
                    .eq(MallShopsEntity::getUserId, getUserId()));
            if (shopsEntity == null || StringUtils.isBlank(shopsEntity.getId())) {
                return RestResponse.error("没有权限，请联系管理员！");
            }
            params.put("parentId", shopsEntity.getId());
        } else {
            params.put("sysUserId", getUserId());
        }
        List<MallOrderEntity> list = mallOrderService.export(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 查询我的所有订单
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/queryMyOrder")
    @RequiresPermissions("mall:order:myOrder")
    public RestResponse myOrderList(@RequestParam Map<String, Object> params) {
        params.put("sysUserId", getUserId());
        List<MallOrderEntity> list = mallOrderService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 发货
     *
     * @param orderEntity
     * @return
     */
    @SysLog("发货")
    @RequestMapping("/sendGoods")
    @RequiresPermissions("mall:order:sendGoods")
    public RestResponse sendGoods(@RequestBody MallOrderEntity orderEntity) {
        mallOrderService.sendGoods(orderEntity);

        return RestResponse.success();
    }

    /**
     * 订单状态变更
     *
     * @param id
     * @return
     */
    @SysLog("订单状态变更")
    @RequestMapping("/statusChange")
    public RestResponse statusChange(String id,Integer status) {
        ValidUtils.allNotNull(id,status);
        mallOrderService.statusChange(id,status,null);
        return RestResponse.success();
    }

    /**
     * 确定收货
     *
     * @param id
     * @return
     */
    @SysLog("确定收货")
    @RequestMapping("/confirmReceive")
    @RequiresPermissions("mall:order:confirmReceive")
    public RestResponse confirmReceive(String id) {
        mallOrderService.confirmReceive(id);

        return RestResponse.success();
    }

    /**
     * 修改价格
     *
     * @return
     */
    @SysLog("修改价格")
    @RequestMapping("/modPrice")
    @RequiresPermissions("mall:order:modPrice")
    public RestResponse modPrice(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        MallOrderEntity orderEntity = mallOrderService.getById(id);
        orderEntity.setActualPrice(new BigDecimal(params.get("actualPrice")));
        mallOrderService.updateById(orderEntity);
        return RestResponse.success();
    }


    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/exportByCard")
    @RequiresPermissions("mall:order:list")
    public RestResponse exportByCard(@RequestParam Map<String, Object> params) {
//        params.put("orderStatuslist", "0,201,300,301,401,402");
        List<MallOrderEntity> list = mallOrderService.exportByCard(params);

        return RestResponse.success().put("list", list);
    }
}
